import streamlit as st
import pandas as pd
from data_loader import DataLoader
from data_processor import DataProcessor
from visualizer import Visualizer
from model import ProductivityModel
from report_generator import ReportGenerator

# 初始化模块
loader = DataLoader()
processor = DataProcessor()
visualizer = Visualizer()
model = ProductivityModel()
reporter = ReportGenerator()

st.title("新质生产力分析平台 📊")

# 数据上传
uploaded_file = st.file_uploader("上传CSV数据", type="csv")
if uploaded_file:
    df = loader.load_csv(uploaded_file)
    st.write("原始数据预览：", df.head())

    # 数据处理
    df_cleaned = processor.clean_data(df)
    df_index = processor.calculate_green_index(df_cleaned, 'GDP Growth', 'Tech Investment', 'CO2 Emissions')
    df_features = processor.add_features(df_index, 'Year')

    # 可视化
    st.subheader("绿色生产力指数趋势")
    visualizer.plot_trend(df_features, 'Year', 'GreenProductivityIndex')

    st.subheader("指标相关性分析")
    visualizer.plot_heatmap(df_features, ['GreenProductivityIndex', 'GDP Growth', 'Tech Investment', 'CO2 Emissions'])

    # 模型预测
    X = df_features[['GDP Growth', 'Tech Investment', 'CO2 Emissions']]
    y = df_features['GreenProductivityIndex']
    trained_model = model.train(X, y)

    # 预测未来趋势
    future_data = pd.DataFrame([[7.5, 8.0, 2.1], [8.0, 8.5, 1.9]], columns=X.columns)
    predictions = model.predict(future_data)
    st.write("未来预测结果：", predictions)

    # 生成报告
    if st.button("生成分析报告"):
        reporter.generate(df_features, predictions)
        st.success("报告生成成功！")